package com.Studen.Dao;

import com.Studen.bean.LoginFile;
import com.Studen.servlet.TeacherLoginSerMpl;
import com.Studen.util.Jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TeacherLoginDaoMpl implements TeacherLoginDao{
    @Override
    public void Login_test(LoginFile file) {
        /*
        *   1、判断controller层传来的数据
        *       如果数据合理：
        *           a、账户不为空
        *           b、账户不为空
        *           c、账户不为空
        *       loginData(file)方法返回为3则数据合理
        *       将LoginFile里的数据加入到数据库里：jdbc六部曲
        *
        * */
        Connection connection = new Jdbc().Get();
        PreparedStatement statement=null;
        String sql="INSERT INTO u_user(u_pwd,u_name,u_sex,u_role,u_isdelete) VALUES(?,?,?,?,?)";

        int i = new TeacherLoginSerMpl().loginData(file);
        if (i==3){
            try {
                statement=connection.prepareStatement(sql);
                statement.setString(1, file.getPwd());
                statement.setString(2, file.getName());
                statement.setInt(3,file.getSex());
                statement.setInt(4,1);
                statement.setInt(5,1);
                statement.executeUpdate();

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
            }
        }
    }
}
